<?php

/**
 * order actions.
 *
 * @package    briefing
 * @subpackage order
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class orderActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  { 
    $this->categories = Doctrine_Core::getTable('Textcategories')->getWithOrders();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->order = $this->getRoute()->getObject();
  }

  public function executeNew(sfWebRequest $request)
  {
    
//    $client_order = new Clientorder();
//    $client_order->set('targetgroupid', 2);//Default target group = Studenten(id = 2)
    
    
//    $this->form = new OwnchoiceForm();
    $this->form = new ClientOrderForm();//$client_order);
//    $this->form->getFormFieldSchema()->current()->getValue()
    
//    $this->form->getFormFieldSchema()->current()->__toString()
  }

  public function executeCreate(sfWebRequest $request)
  {
//    $this->form = new OwnchoiceForm();
    $this->form = new ClientOrderForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
      
    
//    $this->form = new OwnchoiceForm($this->getRoute()->getObject());
      $this->form = new ClientOrderForm($this->getRoute()->getObject());
      
      //as default values the ones of hte DDBB
      $this->form->setDefault('freetextid',$this->form->getObject()->getFreetext());
      $this->form->setDefault('othertextmediumid',$this->form->getObject()->getOthertextmedium());
      
      //se ve que si se hace con setDefaults hay que escribir todos los parametros 
      //porque sino salen vacios.
//      $this->form->setDefaults(array(
//          'freetextid' => $this->form->getObject()->getFreetext(),
//          'othertextmediumid' => $this->form->getObject()->getOthertextmedium(),
//      ));

  }

  public function executeUpdate(sfWebRequest $request)
  {
//    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
//    $this->forward404Unless($client_order = Doctrine_Core::getTable('ClientOrder')->find(array($request->getParameter('idorder'))), sprintf('Object client_order does not exist (%s).', $request->getParameter('idorder')));
    
//    $this->form = new OwnchoiceForm($this->getRoute()->getObject());
    $this->form = new ClientOrderForm($this->getRoute()->getObject());

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

//    $this->forward404Unless($client_order = Doctrine_Core::getTable('ClientOrder')->find(array($request->getParameter('idorder'))), sprintf('Object client_order does not exist (%s).', $request->getParameter('idorder')));
    $client_order = $this->getRoute()->getObject();
    $client_order->delete();

    $this->redirect('order/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind(
            $request->getParameter($form->getName()), 
            $request->getFiles($form->getName())
    );
    if ($form->isValid())
    {
      //new...
//      $client_order = new Ownchoice();
      //---
      
      $client_order = $form->save();
      
      //$form->addCSRFProtection();
      
      //++++
      $this->redirect($this->generateUrl('order_show_order',$client_order));//'order/edit?idorder='.$client_order->getIdorder());
    }
    else{
//      print_r($form);
//      $this->redirect("www.google.es");
    }
  }
  
  public function executePublish(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $order = $this->getRoute()->getObject();
    $order->publish();

    $this->getUser()->setFlash('notice', sprintf('Your order is now online for %s days.', sfConfig::get('app_active_days')));

    $this->redirect($this->generateUrl('order_show_user', $order));
  }
}
